<?php
//gera xml
require_once 'Dataset.php';
$xml = new DOMDocument("1.0","UTF-8");
$class = $xml->createElement('class');
$object = $xml->createElement('object', '');
$object->setAttributeNode(new DOMAttr('id', $_POST['object']));
$class->appendChild($object);
$xml->appendChild($class);
$chaves = array_keys($_POST);
for ($i = 0; $i < count($_POST)-3;$i++ ) {
    $object = $xml->createElement('atribute', '');
    if ($_POST[$chaves[$i]] != '')
      $object->setAttributeNode(new DOMAttr('var', $_POST[$chaves[$i]]));
    else
      $object->setAttributeNode(new DOMAttr('var',$chaves[$i]));
    $object->setAttributeNode(new DOMAttr('field', $chaves[$i]));
    $ds = new DataSet();
    $object->setAttributeNode(new DOMAttr('type',$ds->typeField($_POST['table'], $chaves[$i])->type ));
    $object->setAttributeNode(new DOMAttr('size',$ds->typeField($_POST['table'], $chaves[$i])->size ));
    $object->setAttributeNode(new DOMAttr('scale',$ds->typeField($_POST['table'], $chaves[$i])->scale ));
    $object->setAttributeNode(new DOMAttr('notNull',$ds->typeField($_POST['table'], $chaves[$i])->NotNull ));
    $class->appendChild($object);
}
$xml->save("map/".$_POST['table'].".xml");

echo 'Criado Mapeamento de banco de dados para Dataset.php'."\n";

//geraclasse php

$phpClass = fopen($_POST['object'].'.php', 'w+');
if (!$phpClass)
    echo 'não foi possível criar a classe php'."\n";
$conteudo = "<?php \nclass ".$_POST['object']."{\n  var ";
fwrite($phpClass, $conteudo);
$i = 0;
$item = $xml->getElementsByTagName('atribute')->item($i);
while ($item != null) {
    $campo = $item->getAttribute('var');
    if ($i == 0)
        fwrite($phpClass, '$'.$campo);
    else
        fwrite($phpClass, ',$'.$campo);
    $i++;
    $item = $xml->getElementsByTagName('atribute')->item($i);
}
fwrite($phpClass, ";\n");
fwrite($phpClass, '  function '.$_POST['object']."(){\n\n  }\n\n");
fwrite($phpClass,"}");
fclose($phpClass);



//gera classe flex
$asClass = fopen($_POST['object'].'.as', 'w+');
$objeto = $_POST['object'];
$conteudo =
        " package VO
{
	[Bindable]\n
	[RemoteClass(alias=\"$objeto\")]\n

	public class $objeto
	{\n";

$i = 0;
$item = $xml->getElementsByTagName('atribute')->item($i);
while ($item != null) {
    $campo = $item->getAttribute('var');
    $tipo = $item->getAttribute('type');
    $tipoCampo;
    echo $tipo;
    switch ($tipo){
       case 'VARCHAR':
           $tipoCampo = 'String';
           break;
       case 'INTEGER':
           $tipoCampo = 'int';
           break;
       case 'SMALLINT':
           $tipoCampo = 'int';
           break;
       case 'DOUBLE PRECISION':
           $tipoCampo = 'Number';
           break;
       case 'NUMERIC':
           $tipoCampo = 'Number';
       default :
           $tipoCampo = 'String';

    }
    $conteudo .= '           public var '.$campo.":$tipoCampo;\n";
    $i++;
    $item = $xml->getElementsByTagName('atribute')->item($i);
}

$conteudo.="            public function $objeto(){\n
			
	    }\n\n


	}\n\n
}";

fwrite($asClass, $conteudo);
fclose($asClass);

//header('location:MapBase.php');
?>
